﻿// -----------------------------------------------------------------------
// <copyright file="AsmParser.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Sigma.DrBlue.WpfUI.LibCore
{
    using System.Collections;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Reflection;
    using System.Resources;
    using Sigma.DrBlue.WpfUI.Models;
    using System.Globalization;
    using System.Linq;
    using System;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class AsmParser
    {
        public static AssemblyModel ParseResourceFromAssembly(string file, List<CultureInfo> lstCultures)
        {
            Debug.Assert(File.Exists(file), "File {0} does not exist.", file);

            FileInfo fileInfo = new FileInfo(file);
            AssemblyModel asmModel = new AssemblyModel
            {
                Name = fileInfo.Name,
                FileName = fileInfo.FullName
            };

            Assembly asm = Assembly.LoadFrom(file);
            string[] resourceNames = asm.GetManifestResourceNames();
            foreach (string name in resourceNames)
            {
                ResourceModel resource = new ResourceModel
                {
                    Name = name
                };

                Stream stream = asm.GetManifestResourceStream(name);
                ResourceReader reader = new ResourceReader(stream);
                IDictionaryEnumerator dict = reader.GetEnumerator();

                ResourceManager resManager = new ResourceManager(name.Replace(".resources", string.Empty), asm);

                while (dict.MoveNext())
                {
                    try
                    {
                        if (dict.Value.GetType() == typeof(string))
                        {
                            StringResourceModel stringResource = new StringResourceModel();
                            stringResource.Key = dict.Key.ToString();

                            foreach (CultureInfo culture in lstCultures)
                            {
                                stringResource.Values.Add(culture.Name, resManager.GetString(stringResource.Key, culture));
                            }

                            resource.StringResources.Add(stringResource);
                        }
                    }
                    catch
                    {

                    }
                }

                asmModel.Resources.Add(resource);
            }

            return asmModel;
        }

        public static List<CultureInfo> ParseCulturesFromFolder(string path)
        {
            Debug.Assert(Directory.Exists(path));

            List<CultureInfo> lstCultures = new List<CultureInfo>();

            CultureInfo[] cultures = CultureInfo.GetCultures(CultureTypes.AllCultures);

            string[] directories = Directory.GetDirectories(path);
            foreach (string dir in directories)
            {
                DirectoryInfo dirInfo = new DirectoryInfo(dir);

                CultureInfo culture = (from c in cultures where c.Name.Equals(dirInfo.Name, StringComparison.InvariantCultureIgnoreCase) select c).SingleOrDefault();
                if (culture != null)
                {
                    lstCultures.Add(culture);
                }
            }

            return lstCultures;
        }
    }
}
